/**
 * 
 */
package gameEngine;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ActionMap;
import javax.swing.GroupLayout;
import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.GroupLayout.ParallelGroup;

import org.jdesktop.application.Action;
import org.jdesktop.application.ApplicationContext;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.ResourceMap;

/**
 * @author Joe R. Nassimian
 * @author Yannis Mazzer
 */
public class FirstView extends FrameView implements ActionListener {

	// Variables declaration - do not modify
	private Engine app;
	private ApplicationContext appContext;
	private StatusBar statusBar;
	private JMenu settingsMenu;
    private JMenuItem newGame;
    private JPanel mainPanel;
    private JMenuBar menuBar;
    private JDialog aboutBox;
    private BoardView boardViewer;
    private JalokusNewGame newGameDialog;
    // End of variables declaration    
	
	public FirstView(Engine _application) {
		super(_application);
		this.app = _application;
		this.appContext = _application.getContext();

		mainPanel = new JPanel();
        mainPanel.setName("mainPanel"); // NOI18N

		newGameDialog = new JalokusNewGame(this.getFrame(),app);
		this.app.show(newGameDialog);
		
		buildGUI();
		 // status bar initialization - message timeout, idle icon and busy animation, etc
        //ResourceMap resourceMap = getResourceMap();
        //int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");

	}

	private void buildGUI() {
		statusBar = new StatusBar();
		statusBar.setMessage("Loading ...");
        this.setStatusBar(statusBar);
        
        buildGUIMenu();
        
        boardViewer = new BoardView(app);
        app.getBoardGame().getBoard().addObserver(boardViewer);
        app.getBoardGame().getBoard().updateView();

        
        buildLayout();

    }
	
	private void buildLayout() {
        GroupLayout mainPanelLayout = new GroupLayout(mainPanel);
        int nbPlayers = app.getNbPlayers();
        Player[] players = app.getPlayers();
        
        PiecesView piecesViewers[] = new PiecesView[nbPlayers];
        for(int i = 0 ; i < nbPlayers; i++){
        	piecesViewers[i] = new PiecesView(players[i].getPiecesList(), app);
        }
        
        mainPanel.setLayout(mainPanelLayout);
        int nbPlayersG = 0;
        int nbPlayersD = 0;

        if(nbPlayers % 2 == 0) {
        	nbPlayersG = nbPlayers/2;
        	nbPlayersD = nbPlayers/2;
        }
        else {
        	nbPlayersG = nbPlayers/2;
        	nbPlayersD = (nbPlayers/2)+1;
        }
        
        /**
         * Setting Horizontal layout
         */
        /*
        SequentialGroup hsGroup = mainPanelLayout.createSequentialGroup();
        
        if(nbPlayersG >= 1) {
        	hsGroup.addContainerGap();
        	for(int i = 0; i < nbPlayersG; i++) {
        		pGroup1.addComponent(piecesViewers[i]);
        	}
        	hsGroup.addGroup(pGroup1);
        }
        
        hsGroup.addContainerGap();
		hsGroup.addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(boardViewer)
				);
        
		ParallelGroup pGroup2 = mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        if(nbPlayersD >= 1) {
        	hsGroup.addContainerGap();
        	for(int i = 0; i < nbPlayersD; i++) {
        		pGroup2.addComponent(piecesViewers[i+2]);
        	}
        	hsGroup.addGroup(pGroup2);
        }	
        hsGroup.addContainerGap();
        mainPanelLayout.setHorizontalGroup(hsGroup);
        */
        
        ParallelGroup pGroup1 = mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        ParallelGroup pGroup2 = mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        
        for(int i = 0; i < nbPlayersG; i++) {
    		pGroup1.addComponent(piecesViewers[i]);
    	}
        
        for(int i = 0; i < nbPlayersD; i++) {
    		pGroup2.addComponent(piecesViewers[i+2]);
    	}
        
        mainPanelLayout.setHorizontalGroup(
        		mainPanelLayout.createParallelGroup()
        		.addGroup(mainPanelLayout.createSequentialGroup()
        				.addContainerGap()
        				//.addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
        				//		.addComponent(piecesViewers[0])
        				//		.addComponent(piecesViewers[1]))
        				.addGroup(pGroup1)
        				.addContainerGap()
        				.addComponent(boardViewer)
        				.addContainerGap()
        				.addGroup(pGroup2)
        				//.addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
        				//	.addComponent(piecesViewers[2])
        				//	.addComponent(piecesViewers[3])
        				//)
        				.addContainerGap()
        			)
           // mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            //.addGap(0, 400, Short.MAX_VALUE)
        );
        
        
        /**
         * Setting vertical layout
         */
        /*
        SequentialGroup vsGroup = mainPanelLayout.createSequentialGroup();
        
        
        if(nbPlayersG >= 1) {
        	vsGroup.addContainerGap();
        	
        	
        	vsGroup.addGroup(pGroup3);
        }
        
        vsGroup.addComponent(boardViewer);
        
        if(nbPlayersD >= 1) {
        	vsGroup.addContainerGap();
        	
        	vsGroup.addGroup(pGroup4);
        }	
        vsGroup.addContainerGap();
        
        mainPanelLayout.setVerticalGroup(vsGroup);
        */
        
        ParallelGroup pGroup3 = mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        ParallelGroup pGroup4 = mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        
        for(int i = 0; i < nbPlayers; i+=2) {
    		pGroup3.addComponent(piecesViewers[i]);
    	}
        
        for(int i = 0; i < nbPlayers; i+=2) {
    		pGroup4.addComponent(piecesViewers[i+1]);
    	}
        
        mainPanelLayout.setVerticalGroup(
        		mainPanelLayout.createParallelGroup()
        		.addGroup(mainPanelLayout.createSequentialGroup()
        				.addContainerGap()
        				.addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
        						.addComponent(boardViewer)
        						.addGroup(mainPanelLayout.createSequentialGroup()
        								//.addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
        								//		.addComponent(piecesViewers[0])
        								//		.addComponent(piecesViewers[2])
        								//)
        								.addGroup(pGroup3)
        								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        								.addGroup(pGroup4)
        								//.addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
        								//		.addComponent(piecesViewers[1])
        								//		.addComponent(piecesViewers[3])
        								//)
        						)
        				)
        				.addContainerGap()
        						
        			)
            //mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            //.addGap(0, 250, Short.MAX_VALUE)
        );
        
        setComponent(mainPanel);
	}
	
	private void buildGUIMenu(){

        menuBar = new JMenuBar();
        menuBar.setName("menuBar"); // NOI18N
    	ResourceMap resourceMap = appContext.getResourceMap(FirstView.class);
		ActionMap actionMap = appContext.getActionMap(FirstView.class, this);
    	
        	JMenu gameMenu = new JMenu();
        	gameMenu.setMnemonic('G');
        	gameMenu.setText(resourceMap.getString("gameMenu.text")); // NOI18N
        	gameMenu.setName("gameMenu"); // NOI18N
        
        		newGame = new JMenuItem();
        		newGame.setText(resourceMap.getString("newGame.text")); // NOI18N
        		newGame.setName("newGame"); // NOI18N
        		gameMenu.add(newGame);
        
        		JMenuItem exitMenuItem = new JMenuItem();
        		exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        		exitMenuItem.setName("exitMenuItem"); // NOI18N
        		gameMenu.add(exitMenuItem);

        	menuBar.add(gameMenu);

        	settingsMenu = new JMenu();
        	settingsMenu.setMnemonic('S');
        	settingsMenu.setText(resourceMap.getString("settingsMenu.text")); // NOI18N
        	settingsMenu.setName("settingsMenu"); // NOI18N
        
        	menuBar.add(settingsMenu);
        
        	JMenu helpMenu = new JMenu();
        	helpMenu.setMnemonic('H');
        	helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        	helpMenu.setName("helpMenu"); // NOI18N

        		JMenuItem aboutMenuItem = new JMenuItem();
        		aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        		aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        		helpMenu.add(aboutMenuItem);

        	menuBar.add(helpMenu);
        setMenuBar(menuBar);
	}
	
	@Action
	public void showAboutBox(){
		if (aboutBox == null) {
			aboutBox = new JalokusAbout(this.getFrame());
			aboutBox.setLocationRelativeTo(this.getFrame());
		}
	        app.show(aboutBox);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
}
